﻿<?php

/**

 * 会员中心——积分兑换信息
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.com
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */




defined('ByShopWWI') or exit('Access Invalid!');

 

class member_pointorderControl extends BaseMemberControl{

    public function __construct() {

        parent::__construct();

        //读取语言包

        Language::read('member_member_points,member_pointorder');

        //判断系统是否开启积分和积分兑换功能

        if (C('points_isuse') != 1 || C('pointprod_isuse') != 1){

            showDialog(L('member_pointorder_unavailable'),urlShop('member', 'home'),'error');

        }

        Tpl::output('act', 'member_points');

    }

    public function indexOp() {
        $this->orderlistOp();
    }

    /**

     * 兑换信息列表

     */

    public function orderlistOp() {

        //兑换信息列表
        $where = array();
        $where['point_buyerid'] = $_SESSION['member_id'];
        
        $model_pointorder = Model('pointorder');
        $order_list = $model_pointorder->getPointOrderList($where, '*', 10, 0, 'point_orderid desc');
        $order_idarr = array();
        $order_listnew = array();
        if (is_array($order_list) && count($order_list)>0){
            foreach ($order_list as $k => $v){
                $order_listnew[$v['point_orderid']] = $v;
                $order_idarr[] = $v['point_orderid'];
            }
        }                                                                                                                                                                                                                                                                                                                                                                       

        //查询兑换商品
        if (is_array($order_idarr) && count($order_idarr)>0){
            $prod_list = $model_pointorder->getPointOrderGoodsList(array('point_orderid'=>array('in',$order_idarr)));
            if (is_array($prod_list) && count($prod_list)>0){
                foreach ($prod_list as $v){
                    if (isset($order_listnew[$v['point_orderid']])){
                        $order_listnew[$v['point_orderid']]['prodlist'][] = $v;
                    }
                }
            }
        }

        //信息输出
        Tpl::output('order_list',$order_listnew);
        Tpl::output('page',$model_pointorder->showpage(2));
        self::profile_menu('pointorder','orderlist');
        Tpl::showpage('member_pointorder');

    }

    /**

     *  取消兑换

     */

    public function cancel_orderOp(){

        $model_pointorder = Model('pointorder');

        //取消订单

        $data = $model_pointorder->cancelPointOrder($_GET['order_id'],$_SESSION['member_id']);

        if ($data['state']){

            showDialog(L('member_pointorder_cancel_success'),'index.php?act=member_pointorder','succ');

        }else {

            showDialog($data['msg'],'index.php?act=member_pointorder','error');

        }

    }

    /**

     * 确认收货

     */

    public function receiving_orderOp(){

        $data = Model('pointorder')->receivingPointOrder($_GET['order_id']);

        if ($data['state']){

            showDialog(L('member_pointorder_confirmreceiving_success'),'index.php?act=member_pointorder','succ');

        }else {

            showDialog($data['msg'],'index.php?act=member_pointorder','error');

        }

    }

    /**

     * 从第三方取快递信息

     *

     */

    public function get_expressOp(){

    

        $content = Model('express')->get_express($_GET['e_code'], $_GET['shipping_code']);

    

        $output = array();

        foreach ($content as $k=>$v) {

            if ($v['time'] == '') continue;

            $output[]= $v['time'].'&nbsp;&nbsp;'.$v['context'];

        }

        if (empty($output)) exit(json_encode(false));

    

        echo json_encode($output);

    }

	

	/**

     * 红白积分转换信息详细

     */

	public function memberpointsOp(){

		

		Language::read('member_home_member');

        

		$lang   = Language::getLangContent();



        $model_member   = Model('member');

		

		$red_points=$_GET['red_points'];

		

		

       // $red_points=substr($red_points,0,strlen($red_points)-1);		

		//查询红积分数量

		//$red_points_y  shopwwi_returnpoint

		

		//$sql="select * from ";

		$obj_member2 = Model('member');	

		

	    $member_jf=$obj_member2->where(array('member_id'=>$_SESSION['member_id']))->select();

		

		//exit(print_r($member_jf[0]['member_id']));

		

		//exit(print_r($member_tj));

		

		//

		$returnpoints = Model('returnpoint');	

		$order=' order by addtime desc';

	    $repoint=$returnpoints->order($order)->select();

		$repoints=$repoint['back_point'];

		$now_red_points=$member_jf[0]['member_red_points'];

		

		

		$shengyu_points=$now_red_points-$red_points;

		

		//print_r($now_red_points);

		

		//print_r($shengyu_points);

		

		$pl_addtime=strtotime(date('Y-m-d H:i:s'));

		

		//print_r($red_points);

		//exit();

		

		//if($now_red_points > $red_points){

			

		if($now_red_points >= $red_points){//红积分提现条件:积分大于10000且提现比例为100的倍数

			

			//print_r();

			

			if($red_points % 100 == 0){

			

			$red_pointcash=($red_points*$repoints)/100;//红积分提现比例

		

		    $member_array   = array();

		  // Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$red_pointcash.' where member_id='.$_SESSION['member_id']);

        

		    $member_array['available_predeposit']    = $member_jf[0]['available_predeposit'] + $red_points/100;////增加余额

		

		    $update = $model_member->editMember(array('member_id'=>$_SESSION['member_id']),$member_array);

			

			

			//减去红积分

			 Model()->execute('update shopwwi_member set member_red_points = member_red_points - '.$red_points.' where member_id='.$_SESSION['member_id']);

			 $memberid=$member_jf[0]['member_id'];

			 $membername=$member_jf[0]['member_name'];

			 //增加余额

			$add_yue=$red_points/100;

			//Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$add_yue.' where member_id='.$_SESSION['member_id']);

			//余额增加日志

			$sql_bc = "insert into shopwwi_pd_log (lg_member_id,lg_member_name,lg_type,lg_av_amount,lg_add_time,lg_desc) values($memberid,'$membername','redtocash',". $add_yue .",$pl_addtime,'分红豆变现转入余额-余额增加')";

			Model() -> execute($sql_bc);

			 

			 

			 //积分操作日志

			 $back_points=-$red_points;

			 $sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values($memberid,'$membername',$back_points,$pl_addtime,'分红豆变现转入余额-分红豆减少','redpoints')";

			 Model()->execute($sql_bc);

			

			}

			

			}

			

		  //}



        $message = $update? $lang['nc_common_save_succ'] : $lang['nc_common_save_fail'];

        

		showDialog($message,'reload',$update ? 'succ' : 'error');



		}

	

    /**

     * 兑换信息详细

     */

    public function order_infoOp(){

        $order_id = intval($_GET['order_id']);

        if ($order_id <= 0){

            showDialog(L('member_pointorder_parameter_error'),'index.php?act=member_pointorder','error');

        }

        $model_pointorder = Model('pointorder');

        //查询兑换订单信息

        $where = array();

        $where['point_orderid'] = $order_id;

        $where['point_buyerid'] = $_SESSION['member_id'];

        $order_info = $model_pointorder->getPointOrderInfo($where);

        if (!$order_info){

            showDialog(L('member_pointorder_record_error'),'index.php?act=member_pointorder','error');

        }

        //获取订单状态

        $pointorderstate_arr = $model_pointorder->getPointOrderStateBySign();

        Tpl::output('pointorderstate_arr',$pointorderstate_arr);



        //查询兑换订单收货人地址

        $orderaddress_info = $model_pointorder->getPointOrderAddressInfo(array('point_orderid'=>$order_id));

        Tpl::output('orderaddress_info',$orderaddress_info);



        //兑换商品信息

        $prod_list = $model_pointorder->getPointOrderGoodsList(array('point_orderid'=>$order_id));

        Tpl::output('prod_list',$prod_list);



        //物流公司信息

        if ($order_info['point_shipping_ecode'] != ''){

            $data = Model('express')->getExpressInfoByECode($order_info['point_shipping_ecode']);

            if ($data['state']){

                $express_info = $data['data']['express_info'];

            }

            Tpl::output('express_info',$express_info);

        }



        Tpl::output('order_info',$order_info);

        Tpl::output('left_show','order_view');

        Tpl::showpage('member_pointorder_info');

    }

    /**

     * 用户中心右边，小导航

     *

     * @param string    $menu_type  导航类型

     * @param string    $menu_key   当前导航的menu_key

     * @return

     */

    private function profile_menu($menu_type,$menu_key='') {

        $menu_array = array();

        switch ($menu_type) {

            case 'pointorder':

                $menu_array = array(
            1=>array('menu_key'=>'consume',  'menu_name'=>'消费豆分明细', 'menu_url'=>'index.php?act=member_consume&op=index'),
            2=>array('menu_key'=>'points',  'menu_name'=>'宝豆明细',    'menu_url'=>'index.php?act=member_points'),
            3=>array('menu_key'=>'red_points',  'menu_name'=>'分红豆明细',    'menu_url'=>'index.php?act=reds_member_points'),
			4=>array('menu_key'=>'kucun_points',  'menu_name'=>'商家库存积分明细',    'menu_url'=>'index.php?act=kucun_store_points'),
			5=>array('menu_key'=>'orderlist','menu_name'=>'商品积分兑换',    'menu_url'=>'index.php?act=member_pointorder&op=orderlist')
		
			    );

                break;

            case 'pointorderinfo':

                $menu_array = array(

                    1=>array('menu_key'=>'points',  'menu_name'=>'积分明细',    'menu_url'=>'index.php?act=member_points'),

                    2=>array('menu_key'=>'orderlist','menu_name'=>Language::get('nc_member_path_pointorder_list'),  'menu_url'=>'index.php?act=member_pointorder&op=orderlist'),

                    3=>array('menu_key'=>'orderinfo','menu_name'=>Language::get('nc_member_path_pointorder_info'),  '')

                );

                break;

        }

        Tpl::output('member_menu',$menu_array);

        Tpl::output('menu_key',$menu_key);

    }

}

